package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.MoneyTypeMapper;
import com.mysterymall.Mapper.PaymentClassMapper;
import com.mysterymall.Model.Entity.PaymentClass;
import com.mysterymall.Service.PaymentClassService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/12/18  11:02
 */
@Service
public class PaymentClassServiceImpl implements PaymentClassService {

    private final PaymentClassMapper paymentClassMapper;

    private final MoneyTypeMapper moneyTypeMapper;

    /**
     * 构造器注入
     * @param paymentClassMapper 支付方式映射器
     */
    public PaymentClassServiceImpl(PaymentClassMapper paymentClassMapper, MoneyTypeMapper moneyTypeMapper) {
        this.paymentClassMapper = paymentClassMapper;
        this.moneyTypeMapper = moneyTypeMapper;
    }

    @Override
    public Result getPaymentClass() {
        LambdaQueryWrapper<com.mysterymall.Model.Entity.PaymentClass> queryWrapper = new LambdaQueryWrapper<>();

        List<String> ids = new ArrayList<>();

        ids.add("947926e8-62d2-0f0f-6527-e2d0c5414043");

        // 按金额从小到大排序
        queryWrapper.orderByAsc(com.mysterymall.Model.Entity.PaymentClass::getAmount);
        queryWrapper.notIn(PaymentClass::getId,ids);

        List<PaymentClass> paymentClasses = paymentClassMapper.selectList(queryWrapper);


        return new Result(HttpStatus.SUCCESS,"message.querySuccess",paymentClasses);
    }

    @Override
    public Result addPaymentClass(BigDecimal paymentAccount) {

        if (!StpUtil.hasPermission("paymentClass:add:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        PaymentClass paymentClass = new PaymentClass();

        paymentClass.setAmount(paymentAccount);

        paymentClass.setMoneyTypeId(moneyTypeMapper.queryByCode("MYR").getId());

        paymentClass.setGiftAmount(new BigDecimal(0));
        paymentClassMapper.insert(paymentClass);

        return new Result(HttpStatus.SUCCESS,"message.querySuccess");
    }

    @Override
    public Result deletePaymentClass(String paymentClassId) {

        if (!StpUtil.hasPermission("paymentClass:delete:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        paymentClassMapper.deleteById(paymentClassId);
        return new Result(HttpStatus.SUCCESS,"message.deleteSuccess");

    }
}
